<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/10/17
 * Time: 7:42 PM
 */
declare(strict_types=1);


namespace application\module\admin\controller;


use application\module\admin\addon\Rbca;
use application\module\admin\addon\rbca\model\AccessModel;
use wshore\core\Response;
use wshore\throws\DatabaseException;
use wshore\throws\db\RecordNotFoundException;

class access extends admin
{


    public function index()
    {
        $this->doLayoutAssign();
        $this->display();
    }

    public function updatename(int $id, string $name)
    {
        try {
            $model = new AccessModel($id);
            $model->name = strpos($name, '%') !== false ? urldecode($name) : $name;
            if (!$model->save()) {
                Response::failure('修改失败', $model->all());
            } else {
                Response::success('修改成功', $model->all());
            }
        } catch (RecordNotFoundException $exception) {
            Response::failure("ID为'{$id}'的权限不存在");
        }
    }

    public function updatecomment(int $id, string $comment)
    {
        try {
            $model = new AccessModel($id);
            $model->comment = strpos($comment, '%') !== false ? urldecode($comment) : $comment;
            if (!$model->save()) {
                Response::failure('修改失败', $model->all());
            } else {
                Response::success('修改成功', $model->all());
            }
        } catch (RecordNotFoundException $exception) {
            Response::failure("ID为'{$id}'的权限不存在");
        }
    }

    public function disable(int $id)
    {
        try {
            $model = new AccessModel($id);
            $model->disable = 1;
            if (!$model->save()) {
                Response::failure('禁用失败', $model->all());
            } else {
                Response::success('禁用成功', $model->all());
            }
        } catch (RecordNotFoundException $exception) {
            Response::failure("ID为'{$id}'的权限不存在");
        }
    }

    public function enable(int $id)
    {
        try {
            $model = new AccessModel($id);
            $model->disable = 0;
            if (!$model->save()) {
                Response::failure('启用失败', $model->all());
            } else {
                Response::success('启用成功', $model->all());
            }
        } catch (RecordNotFoundException $exception) {
            Response::failure("ID为'{$id}'的权限不存在");
        }
    }


    /**
     * 删除资源
     * @param int $id
     * @return void
     */
    public function delete(int $id)
    {
        $model = AccessModel::getInstance();
        $model->beginTransaction();
        if ($model->delete(['id' => $id])) {
            $model->commit();
            Response::success('删除成功', $model->find($id));
        }
        $model->rollback();
        Response::failure('删除失败');
    }

    /**
     * 反射类方法
     */
    public function reflect()
    {
        try {
            Rbca::getInstance()->reflect2access();
            Response::success('反射成功');
        } catch (DatabaseException $exception) {
            Response::failure('反射失败');
        }
    }

    public function getlist()
    {
        $model = new AccessModel();
        try {
            Response::success('', $model->select());
        } catch (DatabaseException $e) {
            Response::failure('获取列表失败');
        }
    }

    public function add()
    {
        $model = AccessModel::getInstance();
        foreach ($_GET as $k => $v) {
            if ('' === $v or strtolower($v) === 'null') unset($_GET[$k]);
        }
        if ($model->insert($_GET)) {
            $lastid = $model->lastInsertId();
            Response::success('添加成功', $model->find($lastid));
        } else {
            Response::failure($model->error());
        }
    }

}